<?php
// -----------------------------------------------------------------------------
// 引入
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";
include "../resources/Snowflake/HighConcurrencySnowflake.php";

// -----------------------------------------------------------------------------
// 访问设置
// -----------------------------------------------------------------------------

if (!isset($_POST["categoryTitle"]) || !isset($_POST["categoryDescription"]) || !isset($_POST["targetSortNumber"])) {
    exit("需要传入有效的参数！");
}

// -----------------------------------------------------------------------------
// 数据库设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath;
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------
// 时区设置
// -----------------------------------------------------------------------------

date_default_timezone_set($region);

// -----------------------------------------------------------------------------

/* 使用雪花算法生成排序号 */
$snowflake = new HighConcurrencySnowflake();
$sortNumber = $snowflake->nextId();

// -----------------------------------------------------------------------------

$timeCreate = (string)time();
$title = $_POST["categoryTitle"];
$description = $_POST["categoryDescription"];
$targetSortNumber = $_POST["targetSortNumber"];

// -----------------------------------------------------------------------------

if ($db->checkIfCategoryExistsByTitle($title)) {
    exit("创建失败，因为该 category 名称已经存在。");
}

// -----------------------------------------------------------------------------

$arr = compact("timeCreate", "sortNumber", "title", "description", "targetSortNumber");

$db->insertIntoCategory($arr);
echo "分类创建成功！";
